/**
 * created by 文阿花, 2021/4/22
 * Copyright (c) 2021, 416143467@qq.com All Rights Reserved.
 * #                   *********                            #
 * #                  ************                          #
 * #                  *************                         #
 * #                 **  ***********                        #
 * #                ***  ****** *****                       #
 * #                *** *******   ****                      #
 * #               ***  ********** ****                     #
 * #              ****  *********** ****                    #
 * #            *****   ***********  *****                  #
 * #           ******   *** ********   *****                #
 * #           *****   ***   ********   ******              #
 * #          ******   ***  ***********   ******            #
 * #         ******   **** **************  ******           #
 * #        *******  ********************* *******          #
 * #        *******  ******************************         #
 * #       *******  ****** ***************** *******        #
 * #       *******  ****** ****** *********   ******        #
 * #       *******    **  ******   ******     ******        #
 * #       *******        ******    *****     *****         #
 * #        ******        *****     *****     ****          #
 * #         *****        ****      *****     ***           #
 * #          *****       ***        ***      *             #
 * #            **       ****        ****                   #
 */
package com.light.mytext.javaknowledge.datastructure;

import android.util.Log;

import java.util.Arrays;

/**
 * 描 述：静态栈（通过数组创建栈）
 * 作 者：文阿花  2021-04-22 09:37
 * 修改描述：
 * 修 改 人：
 * 修改版本：
 */
public  class ListStackText<T> {
    //实现栈的数组
    public Object[] statck;
    public int size;


    public ListStackText() {
        statck = new Object[10];//初始化容量
    }

    //判断是否为空
    public boolean isEmpty() {
        return this.size == 0;
    }

    //返回栈顶元素
    public T getTopData() {
        T t = null;
        if (size > 0) {
            t = (T) statck[size - 1];
        }
        return t;
    }

    //出栈
    public void outStack() {
        T t = getTopData();
        if (t != null) {
            statck[size - 1] = null;
            size--;
        }
    }


    //进栈
    public void pushStack(T t) {
        //先判断容量，如果容量不够则先扩容
        expandCapacity(size + 1);
        statck[size] = t;
        size++;
    }

    //扩容
    public void expandCapacity(int size) {
        int length = statck.length;
        if (length < size) {
            size = size * 2;//每次扩容一倍
            statck = Arrays.copyOf(statck, size);
        }
    }


    public String ToString() {
        StringBuffer buffer = new StringBuffer("");
        for (int i = 0; i < size; i++) {
            buffer.append(statck[i]);
            buffer.append(",");
        }
        buffer.append("NULL");
        return buffer.toString();
    }

    //打印
    public void LogMess() {
        Log.i("LinkTableActivity==", ToString());

    }
}
